package org.example.view;

import org.example.controller.UserController;
import org.example.data.Result;
import org.example.data.User;
import org.example.data.UserMessage;
import org.example.data.UserWithDetails;
import org.example.view.admin.AdminIndex;
import org.example.view.admin.UpdateRoom;
import org.example.view.normal.NormalIndex;

import javax.swing.*;
import java.awt.*;

public class Login extends JFrame {
    private JLabel userLabel, passwordLabel;
    private JTextField userField;
    private JPasswordField passwordField;
    private JButton loginButton, signButton, exitButton;

    public Login() {
        super("欢迎使用酒店管理系统");

        userLabel = new JLabel("用户名：", JLabel.RIGHT);
        userField = new JTextField(15);

        passwordLabel = new JLabel("密码：", JLabel.RIGHT);
        passwordField = new JPasswordField(15);

        loginButton = new JButton("登录");
        signButton = new JButton("注册");

        actionListener();

        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5); // 设置组件间距

        gbc.gridx = 0;
        gbc.gridy = 0;
        panel.add(userLabel, gbc);

        gbc.gridx = 1;
        panel.add(userField, gbc);

        gbc.gridx = 0;
        gbc.gridy = 1;
        panel.add(passwordLabel, gbc);

        gbc.gridx = 1;
        panel.add(passwordField, gbc);

        gbc.gridx = 0;
        gbc.gridy = 2;
        panel.add(loginButton, gbc);

        gbc.gridx = 1;
        panel.add(signButton, gbc);

        this.add(panel);

        // 设置窗口大小不可调整
        this.setResizable(false);
        this.setSize(500,300);
        // 窗口居中显示
        Dimension screen = this.getToolkit().getScreenSize();
        this.setLocation((screen.width - this.getSize().width) / 2, (screen.height - this.getSize().height) / 2);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    private void actionListener() {
        loginButton.addActionListener(e -> {
            String username = userField.getText();
            String password = new String(passwordField.getPassword()); // 输入密码获取方式

            // 进行登录处理
            Result<User> userResult = new UserController().login(username, password);

            if (userResult.getStatus()) {
                User user = userResult.getData();
                Result<UserMessage> userMessageResult = new UserController().getUserMessage(user.getId());

                // 登录成功，跳转到 用户 界面
                if (userMessageResult.getStatus()){
                    if (user.getUsertype().equals("管理员")){
                        //管理员界面
                        System.out.println("显示管理员页面");
                        UserWithDetails user1 = new UserWithDetails(user,userMessageResult.getData());
                        AdminIndex index = new AdminIndex(user1);
                    }else {
                        //用户界面
                        System.out.println("显示用户页面");
                        UserWithDetails user1 = new UserWithDetails(user,userMessageResult.getData());
                        SwingUtilities.invokeLater(() -> new NormalIndex(user1).setVisible(true));
                    }

                }


                dispose(); // 关闭当前登录界面
            } else {
                // 登录失败，弹出错误信息
                JOptionPane.showMessageDialog(Login.this, userResult.getMessage(), "登录失败", JOptionPane.ERROR_MESSAGE);
            }
        });

        signButton.addActionListener(e -> {
            String username = userField.getText();
            String password = new String(passwordField.getPassword()); // 输入密码获取方式

            // 进行注册处理
            Result<Integer> userResult = new UserController().sign(username, password);

            if (userResult.getStatus()) {
                // 注册成功，弹出成功信息
                JOptionPane.showMessageDialog(Login.this, "注册成功", "提示", JOptionPane.INFORMATION_MESSAGE);
            } else {
                // 注册失败，弹出错误信息
                JOptionPane.showMessageDialog(Login.this, userResult.getMessage(), "注册失败", JOptionPane.ERROR_MESSAGE);
            }
        });
    }
}

